package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: WorkspaceServiceImpl
 * @Author 贾金博
 * @Package com.sky.service.impl
 * @Date 2024/3/31 21:13
 * @description:
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();

        //新增总用户人数
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end", end);
        Integer userNumber = userMapper.countByMap(map);
        businessDataVO.setNewUsers(userNumber);

        //查询有效订单数
        Map map2 = new HashMap();
        map2.put("status", Orders.COMPLETED);
        map2.put("begin", begin);
        map2.put("end", end);
        Integer valueOrdersNum = orderMapper.getOrdesCountByMap(map2);
        valueOrdersNum = valueOrdersNum == null ? 0 : valueOrdersNum;
        businessDataVO.setValidOrderCount(valueOrdersNum);

        //查询总的订单数
        Map map3 = new HashMap();
        map3.put("begin", begin);
        map3.put("end", end);
        Integer totalOrderNum = orderMapper.getOrdesCountByMap(map3);
        if(totalOrderNum == 0)totalOrderNum = 1;
        businessDataVO.setOrderCompletionRate(valueOrdersNum * 1.0 / totalOrderNum);

        //营业额
        Map map4 = new HashMap();
        map4.put("status", Orders.COMPLETED);
        map4.put("begin", begin);
        map4.put("end", end);
        Double sumMoney = orderMapper.sumByMap(map4);
        sumMoney = sumMoney == null ? 0 : sumMoney;
        businessDataVO.setTurnover(sumMoney);

        //平均客单价
        businessDataVO.setUnitPrice(sumMoney * 1.0 / totalOrderNum);

        return businessDataVO;
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        Dish dish = new Dish();
        dish.setStatus(1);

        List<Dish> dishes = dishMapper.list(dish);
        Integer soldSize = dishes == null ? 0 : dishes.size();

        dish.setStatus(0);
        dishes = dishMapper.list(dish);
        Integer discontinued = dishes == null ? 0 : dishes.size();

        dishOverViewVO.setSold(soldSize);
        dishOverViewVO.setDiscontinued(discontinued);
        return dishOverViewVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public DishOverViewVO overviewSetmeals() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(0); // 停用
        List<Setmeal> list = setmealMapper.list(setmeal);
        dishOverViewVO.setDiscontinued(list == null ? 0 : list.size());

        setmeal.setStatus(1);
        list = setmealMapper.list(setmeal);
        dishOverViewVO.setSold(list == null ? 0 : list.size());
        return dishOverViewVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        Map map = new HashMap();
        map.put("begin", LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
        //总的
        Integer total = orderMapper.getOrdesCountByMap(map);
        orderOverViewVO.setAllOrders(total == null ? 0 : total );

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancel = orderMapper.getOrdesCountByMap(map);
        orderOverViewVO.setCancelledOrders(cancel == null ? 0 : cancel);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completed = orderMapper.getOrdesCountByMap(map);
        orderOverViewVO.setCompletedOrders(completed == null ? 0 : completed);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer confirmed = orderMapper.getOrdesCountByMap(map);
        orderOverViewVO.setDeliveredOrders(confirmed == null ? 0 : confirmed);

        //待接单
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer toBeConfirmed = orderMapper.getOrdesCountByMap(map);
        orderOverViewVO.setWaitingOrders(toBeConfirmed == null ? 0 : toBeConfirmed);

        return orderOverViewVO;
    }
}
